package tree;

import java.util.ArrayList;
import java.util.List;

/**
 * 递归实现
 */
public class ProOrder {
    public  List<Integer> preorderTraversal(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        if (root != null) {
            preorder(root, list);
        }
        return list;

    }
    private void preorder(TreeNode node, List<Integer> result) {
        if (node == null) {
            return;
        }
        result.add(node.val);
        preorder(node.left, result);
        preorder(node.right, result);
    }

    public static void main(String[] args) {
        // 创建树
        TreeNode node7 = new TreeNode(7, null, null);
        TreeNode node6 = new TreeNode(6, null, null);
        TreeNode node8 = new TreeNode(8, null, null);
        TreeNode node9 = new TreeNode(9, node6, node7);
        TreeNode root = new TreeNode(3, node9, node8);

        // 调用打印方法
        ProOrder proOrder = new ProOrder();
        List<Integer> result = proOrder.preorderTraversal(root);

        // 输出结果
        System.out.println(result);
    }


}
